package smashedapples.microsoft.sqlserver
{
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.utils.ObjectUtil;
	import smashedapples.core.NonVisualComponent;
	import smashedapples.remoting.RemoteObject;
	import smashedapples.microsoft.sqlserver.commands.DeleteCommand;
	import smashedapples.microsoft.sqlserver.commands.SaveCommand;
	import smashedapples.microsoft.sqlserver.commands.SelectCommand;
	import smashedapples.microsoft.sqlserver.responses.SaveResponse;

	
	[Event(name="onSaveResult",type="smashedapples.microsoft.events.SqlServerEvent")]
	[Event(name="onSaveFault",type="smashedapples.microsoft.events.SqlServerEvent")]
	
	
	[DefaultProperty("tables")]
	public class SqlServer extends NonVisualComponent
	{	

		public static const SELECT : String = "select";
		public static const SAVE : String = "save";
		public static const DELETE : String = "delete";
		
		public static const DESTINATION : String = "SmashedApples.Data.SqlServer";
		
		public function SqlServer()
		{
			super();
		}
		
		
		[Inspectable(enumeration="true,false",defaultValue="false")]
		public var debug : Boolean = false;
		
		public var endpoint : String = "";
		
		private var _ro : RemoteObject;
		public function get ro():RemoteObject
		{
			if(_ro == null)
				_ro = new RemoteObject(SqlServer.DESTINATION, this.endpoint);
			return this._ro;
		}
		

		public function save(tableName:String, data : Object , result:Function=null, fault:Function=null):AsyncToken
		{	
			var table : Table = this.getTableByName( tableName );
			var command : SaveCommand = new SaveCommand(table.name,table.pk, data);
				command.debug = this.debug;
			return ro.method(SqlServer.SAVE, result, fault).send(command);
		}
		
		private function onSaveResult(event:ResultEvent):void
		{
			var response : SaveResponse = event.result as SaveResponse;
			
			//trace( ObjectUtil.toString( response.data ));
			
			if( response.debug)
				Debug.push( this, response.info );
		}
		
		private function onSaveFault(event:FaultEvent):void
		{
			Debug.push(this,event.fault);
		}

		public function select(command:SelectCommand, result:Function=null, fault:Function=null):AsyncToken
		{	
			command.debug = this.debug;
			return ro.method(SqlServer.SELECT, result, fault).send(command);
		}
	
		public function del( tableName :String, pks:Array, result:Function=null, fault:Function=null):AsyncToken
		{	
			var table : Table = this.getTableByName( tableName );
			var command : DeleteCommand = new DeleteCommand( table.name, table.pk, pks);
			return ro.method(SqlServer.DELETE, result, fault).send(command);
		}
		
		
		public function getTableByName(name:String):Table
		{
			var table : Table;
			for each( var t : Table in this.tables)
			{
				if(  t.name == name)
				{
					table = t;
					break;
				}
			}
			
			if( table == null)
				throw( new Error("The table you referenced does not exist") );
			
			return table;
		}
		
		
		[ArrayElementType("smashedapples.microsoft.sqlserver.Table")]
		public var tables : Array = new Array();
		
		
		[ArrayElementType("smashedapples.microsoft.sqlserver.DataQuery")]
		public var queries : Array = new Array();
	
		
		
		
	}
}